<?php
/**
 * Geneone: Content Management Framework
 * Object Namespace Search Result Object
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once 'Gene/Object.php';

class Gene_Search_Result_Object {
	var $q;
	var $perPage = 10;
	var $numResults;
	
	/**
	 * Initializes the class.
	 *
	 * @var object $q Database Query
	 * @var string $phrase Search Phrase
	 */
	function Gene_Search_Result_Object($q, $phrase) {
		$this->q =& $q;
		$this->phrase = $phrase;
	}
	
	/**
	 * Sets the number of results to display on a page.
	 *
	 * @var integer $perPage Results per page
	 */
	function setPerPage($perPage) {
		$this->perPage = $perPage;
	}
	
	/**
	 * @return integer total number of results
	 */
	function getNumResults() {
		if ($this->numResults === null) {
			$this->numResults = $this->q->numRows();
		}
		
		return $this->numResults;
	}
	
	/**
	 * @return integer number of pages required to display all results
	 */
	function getNumPages() {
		return ceil($this->getNumResults()/$this->perPage);
	}
	
	/**
	 * Returns results for a specific page.
	 *
	 * @var integer $page Page number
	 *
	 * @return array Search results for page
	 */
	function getPagedResults($page=1) {
		$start = ($page-1)*$this->perPage;
		$this->q->seek($start);
		$results = array();
		
		$i = 0;
		$c = true;
		while ($i < $this->perPage && $c) {
			$row = $this->q->fetchRow();
			
			if ($row) {
				// Check permissions
				if (Gene_Object::checkPermission($row, 'view')) {
					$snippet = str_replace("\n", " ", strip_tags(html_entity_decode($row['content'])));
					
					$terms = str_word_count($this->phrase, 1);
					$snipped = false;
					
					if (strlen($snippet) > 350) {
						// Split into 400 character sections and find most busy one
						$longestsection = 0;
						for ($j = 0; $j < strlen($snippet); $j += 400) {
							$section = substr($snippet, $j, 400);
							$lower = strtolower($section);
							foreach ($terms as $k) {
								$count = substr_count($lower, strtolower($k));
								if ($count > $longestsection) {
									$maxsection = $section;
									$longestsection = $count;
								}
							}
						}
						
						if (!isset($maxsection)) {
							// String wasn't found in content, use first 400.
							$maxsection = substr($snippet, 0, 400);
						}
						
						$first = strpos($maxsection, " ");
						$last = strrpos($maxsection, " ");
						
						$snippet = "...".substr($maxsection, $first, $last-$first)."...";
					}
					
					foreach ($terms as $j) {
						$t = preg_replace("/[^[:alnum:] \-]/e", "", $j);
						$snippet = preg_replace("'([^ ]*".$t."[^ ]*)'i", '<strong>\1</strong>', $snippet);
					}
					
					$results[] = array(
						"title" => $row['title'],
						"snippet" => $snippet,
						"url" => Gene::makeLink($row),
						"extra" => date("F jS Y, H:i", $row['t_create']),
					);
				}
			} else {
				$c = false;
			}
			
			++$i;
		}
		
		return $results;
	}
}
 
?>